package org.lion.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.lion.response.BaseResponse;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JsonUtil {
	
	/**
	 * 将对象转成json串
	 * @param object
	 * @return
	 */
	public static String toJSONString(Object object){
		//DisableCircularReferenceDetect来禁止循环引用检测
		return JSON.toJSONString(object,SerializerFeature.DisableCircularReferenceDetect);
	}
	//输出json
	public static void write_json(HttpServletResponse response,String jsonString)
	{
		response.setContentType("application/json;utf-8");
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().print(jsonString);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

	public static void ajaxResult(HttpServletResponse response,int code, String msg) {
		BaseResponse baseResponse = new BaseResponse();
		baseResponse.setCode(code);
		baseResponse.setMsg(msg);
		write_json(response, toJSONString(baseResponse));
	}
}
